
#ifndef __GD25Q64_H
#define __GD25Q64_H

#include "gd32f1x0.h"
#include "main.h"

#define gd25q64RCU_SPIx    RCU_SPI0
#define gd25q64RCU_GPIOx   RCU_GPIOA
#define gd25q64GPIOx       GPIOA
#define gd25q64GPIO_PIN_x  GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
#define gd25q64SPI_CS_PIN_x  GPIO_PIN_4
 
#define gd25q64DataLen 1024
 
 
#define GD25Q64_WriteEnable      0x06
#define GD25Q64_WriteDisable     0x04
#define GD25Q64_ReadStatusRegister1 0x05  //S7-S0
#define GD25Q64_ReadStatusRegister2 0x35  //S15-S8
#define GD25Q64_ReadStatusRegister3 0x15  //S23-S16
#define GD25Q64_WriteStatusRegister1 0x01 
#define GD25Q64_WriteStatusRegister2 0x31 
#define GD25Q64_WriteStatusRegister3 0x11 
#define GD25Q64_ReadData  0x03
#define GD25Q64_FastRead  0x0B
#define GD25Q64_PageProgram 0x02
#define GD25Q64_SectorErase 0x20
#define GD25Q64_BlockErase32 0x52
#define GD25Q64_BlockErase64 0xD8
#define GD25Q64_ChipErase 0xC7
#define GD25Q64_ReadIdentification 0x9F
#define GD25Q64_DeepPowerDown  0xB9
#define GD25Q64_ManufacturerDeviceID 0x90
#define GD25Q64_ReleaseFromDeepPowerDown 0xAB
#define GD25Q64_ReadIdentification 0x9F

//Program/Erase Speed
#define PageProgramTime 0.6   //ms
#define SectorEraseTime 50
#define BlockEraseTime  200
#define ChipEraseTime   25000

//Flexible Architecture   1M=1024K  1K=1024Byte 1Byte=8bit 
#define GD25Q64_Size       (8*1024*1024)
#define GD25Q64_PageSize    256      //Byte
#define GD25Q64_SectorSize (4*1024) //Byte
#define GD25Q64_BlockSize  (32*1024) //Byte

enum StoragePos
{
	//Info: Sector: 0   usedsize: 1133     
	//----------------------------------//
	Posflechazo = 0, //8B
	PosProgramVersion = Posflechazo+8, //4B
	PosOwnID=PosProgramVersion+4, //8B
	PosFunctionalConfiguration=PosOwnID+8, //1B
	PosRemoteControl=PosFunctionalConfiguration+1, //10B
	PosNumberofequipment=PosRemoteControl+10, //1B
	PosDeviceID=PosNumberofequipment+1, //100 * 11B

	PosIndependentTaskInfo_num=PosDeviceID+(100*11), //1B
	PosGlobalTaskInfo_num=PosIndependentTaskInfo_num+1, //1B
	
	//----------------------------------//
	
	//task: Sector 1-250
	//----------------------------------//
	//Independent task 1k * 1k
	PosTaskIndex_I = 4096, //1B
	PosTaskEnable_I = PosTaskIndex_I+1, //1B
	PosTaskStartTime_I = PosTaskEnable_I+1, //4B
	PosTaskEndTime_I = PosTaskStartTime_I+4, //4B
	PosTaskResources_I = PosTaskEndTime_I+4, //1B
	PosTaskDeviceNum_I = PosTaskResources_I+1, //1B
	PosTaskDeviceID_I = PosTaskDeviceNum_I+1, //9B * 100
	
	
	//Global task : Sector 251
	//----------------------------------//
	//Global task 11B*300    1028096+Index*11  
	PosTaskIndex_G = 1028096, //1B
	PosTaskEnable_G = PosTaskIndex_G+1, //1B
	PosTaskStartTime_G = PosTaskEnable_G+1, //4B
	PosTaskEndTime_G = PosTaskStartTime_G+4, //4B
	PosTaskResources_G = PosTaskEndTime_G+4, //1B
	
};

#define GD25Q64_CS_LOW()			{gpio_bit_write(GPIOA, GPIO_PIN_4,RESET);}
#define GD25Q64_CS_HIGH() 			{gpio_bit_write(GPIOA, GPIO_PIN_4,SET);}


void vgd25q64SPIInit(void);
void Flash_EraseSector(uint32_t _ulEraseAddr);
void Flash_EraseChip(void);

void vgd25q64ReadData(uint8_t *buffer, uint32_t add, uint16_t len);
void vgd25q64WriteData(unsigned  char data[],unsigned int add,unsigned short length);


#endif

